package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.OldmanDepositEntity;
import city.spring.modules.oldman.service.OldmanDepositService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：老人押金
 *
 * @author 谢金婷
 * @date 2020-08-25 11:52:26
 */
@RestController
@RequestMapping("/oldman_deposit")
public class OldmanDepositController extends ApiController {
    private final OldmanDepositService oldmanDepositService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanDepositEntity, ?>> entityOrderFields;

    public OldmanDepositController(OldmanDepositService oldmanDepositService) {
        this.oldmanDepositService = oldmanDepositService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanDepositEntity::getId);
        entityOrderFields.add(OldmanDepositEntity::getOldmanId);
        entityOrderFields.add(OldmanDepositEntity::getItemId);
        entityOrderFields.add(OldmanDepositEntity::getFee);
        entityOrderFields.add(OldmanDepositEntity::getRemark);
    }

    /**
     * 获取全部的 <strong>老人押金</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, OldmanDepositEntity entity) {
        LambdaQueryChainWrapper<OldmanDepositEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>老人押金</strong> 列表
     *
     * @param pageable 分页参数信息
     */
//    @GetMapping
//    public Object list(@PageableDefault Pageable pageable, OldmanDepositVO entity) {
//
//        QueryWrapper<? extends OldmanDepositEntity> queryWrapper = buildQuery(entity);
//        return success(oldmanDepositService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
//    }

    /**
     * 获取一个 <strong>老人押金</strong>
     *
     * @param id 主键ID
     */
//    @GetMapping("{id}")
//    public Object info(@PathVariable String id) {
//
////        return success(oldmanDepositService.getById(id));
//        QueryWrapper<OldmanDepositVO> wrapper = new QueryWrapper<>();
//        wrapper.eq("t1.id", id);
//        List<OldmanDepositVO> records = oldmanDepositService.listPage(new Page<>(), wrapper).getRecords();
//        if (records.isEmpty()) {
//            return success(null);
//        }
//        return success(records.get(0));
//    }

    /**
     * 添加一个 <strong>老人押金</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanDepositEntity entity) {
        oldmanDepositService.saveOldmanDeposit(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>老人押金</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanDepositEntity entity) {
        entity.setId(id);
        oldmanDepositService.updateOldmanDeposit(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>老人押金</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanDepositService.deleteOldmanDeposit(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>老人押金</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanDepositService.deleteOldmanDeposit(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanDepositEntity> buildLambdaQuery(OldmanDepositEntity entity) {
        LambdaQueryChainWrapper<OldmanDepositEntity> lambdaQuery = oldmanDepositService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanDepositEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), OldmanDepositEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getItemId()), OldmanDepositEntity::getItemId, entity.getItemId());
        lambdaQuery.eq(entity.getFee() != null, OldmanDepositEntity::getFee, entity.getFee());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRemark()), OldmanDepositEntity::getRemark, entity.getRemark());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanDepositEntity> buildQuery(OldmanDepositEntity entity) {
        QueryWrapper<OldmanDepositEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "t1.oldman_id", entity.getOldmanId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanName()), "t2.oldman_name", entity.getOldmanName());
        wrapper.like(StringUtils.isNotBlank(entity.getItemId()), "t1.item_id", entity.getItemId());
        wrapper.like(StringUtils.isNotBlank(entity.getName()), "t3.name", entity.getName());
        wrapper.eq(entity.getFee() != null, "t1.fee", entity.getFee());
        wrapper.like(StringUtils.isNotBlank(entity.getRemark()), "t1.remark", entity.getRemark());
        return wrapper;
    }

    /**
     * 根据oldmanId查找老人押金
     *
     * @param oldmanId
     * @return
     */
    @GetMapping("getDepositByOldmanId/{oldmanId}")
    public Object getDepositByOldmanId(@PathVariable String oldmanId) {
        return success(oldmanDepositService.getDepositByOldmanId(oldmanId));
    }

}
